<template>
  <div>
    <!-- Header 区域 -->
    <van-nav-bar fixed title="文章详情" left-arrow @click-left="$router.back()" />
    <van-loading size="24px" vertical class="loading" v-if="!newObj.title">加载中...</van-loading>
    <!-- 文章信息区域 -->
    <div v-else>
      <div class="article-container">
        <!-- 文章标题 -->
        <h1 class="art-title">{{ newObj.title }}</h1>

        <!-- 用户信息 -->
        <van-cell center :title="newObj.aut_name" :label="newObj.pubdate">
          <template #icon>
            <img :src="newObj.aut_photo" alt="" class="avatar" />
          </template>
          <template #default>
            <div>
              <van-button type="info" size="mini" v-if="newObj.is_followed === true" @click="attentionFn(true)"
                >已关注</van-button
              >
              <van-button icon="plus" type="info" size="mini" plain v-else @click="attentionFn(false)">关注</van-button>
            </div>
          </template>
        </van-cell>

        <!-- 分割线 -->
        <van-divider></van-divider>

        <!-- 文章内容 -->
        <div class="art-content" v-html="newObj.content" v-highlight></div>

        <!-- 分割线 -->
        <van-divider>End</van-divider>

        <!-- 点赞 -->
        <div class="like-box">
          <van-button icon="good-job" type="danger" size="small" v-if="newObj.attitude === 1" @click="attitudeFn(true)"
            >已点赞</van-button
          >
          <van-button icon="good-job-o" type="danger" plain size="small" v-else @click="attitudeFn(false)"
            >点赞</van-button
          >
        </div>
      </div>

      <div>
        <van-list v-model="loading" :finished="finished" finished-text="没有更多了" @load="onLoad">
          <CommentList :arr="arr" :isshow="isshow"></CommentList>
        </van-list>
      </div>

      <div :class="isshow ? 'art-cmt-container-1' : 'art-cmt-container-2'">
        <div class="add-cmt-box van-hairline--top" v-if="isshow === true">
          <van-icon name="arrow-left" size="0.48rem" @click="$router.back()" />
          <div class="ipt-cmt-div" @click="isshow = false">发表评论</div>
          <div class="icon-box">
            <van-badge :content="totleCount" max="99">
              <van-icon name="comment-o" size="0.53333334rem" @click="moveFn" />
            </van-badge>
            <van-icon name="star-o" size="0.53333334rem" />
            <van-icon name="share-o" size="0.53333334rem" />
          </div>
        </div>

        <!-- 底部添加评论区域 - 2 -->
        <div class="cmt-box van-hairline--top" v-else>
          <textarea v-fofo placeholder="友善评论、理性发言、阳光心灵" @blur="show" v-model="text"></textarea>
          <van-button type="default" :disabled="text.length === 0" @click="putComm">发布</van-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { PutCommentAPI, getCommentAPI } from '@/api/comment.js'
import { articleDetailAPI, attentionAPI, delAttentionAPI, likeArticleAPI, dislikeArticleAPI } from '@/api/article.js'
import CommentList from '@/views/ArticleDetail/components/CommentList.vue'

export default {
  name: 'AricleDetail',
  data() {
    return {
      newObj: {},
      arr: [],
      isshow: true,
      totleCount: '',
      text: '',
      last_id: null,
      loading: false,
      finished: false
    }
  },
  components: {
    CommentList
  },
  methods: {
    async getcomFn() {
      const res = await getCommentAPI({
        source: this.$route.params.aid,
        offset: this.last_id
      })
      if (res.data.data.results.length === 0) {
        this.finished = true

        return
      }
      this.arr = [...this.arr, ...res.data.data.results]
      this.last_id = res.data.data.last_id
      this.totleCount = res.data.data.total_count || ''
      this.loading = false
    },
    onLoad() {
      this.getcomFn()
    },
    async attentionFn(val) {
      if (val) {
        this.newObj.is_followed = false
        await delAttentionAPI({ target: this.newObj.aut_id })
      } else {
        this.newObj.is_followed = true
        const r = await attentionAPI({ target: this.newObj.aut_id })
        console.log(r)
      }
    },
    async attitudeFn(val) {
      if (val) {
        this.newObj.attitude = -1
        const res = await dislikeArticleAPI({ target: this.newObj.art_id })
        console.log(res)
      } else {
        this.newObj.attitude = 1
        await likeArticleAPI({ target: this.newObj.art_id })
      }
    },
    show() {
      setTimeout(() => {
        this.isshow = true
      })
    },
    async putComm() {
      const res = await PutCommentAPI({ target: this.$route.params.aid, content: this.text })

      // this.getcomFn()
      this.arr.unshift(res.data.data.new_obj)
      this.totleCount++
      this.text = ''
      setTimeout(() => {
        this.moveFn()
      })
    },
    moveFn() {
      document.querySelector('.like-box').scrollIntoView({
        behavior: 'smooth'
      })
    }
  },
  async created() {
    const res = await articleDetailAPI({ id: this.$route.params.aid })
    this.newObj = res.data.data

    this.getcomFn()
  }
}
</script>

<style scoped lang="less">
.loading {
  padding: 100px;
}
.article-container {
  padding: 10px;
  margin-top: 46px;
}
.art-title {
  font-size: 16px;
  font-weight: bold;
  margin: 10px 0;
}

.art-content {
  font-size: 12px;
  line-height: 24px;
  width: 100%;
  overflow-x: scroll;
  word-break: break-all;
  /deep/ img {
    width: 100%;
  }
  /deep/ pre {
    white-space: pre-wrap;
    word-wrap: break-word;
  }
}

.van-cell {
  padding: 5px 0;
  &::after {
    display: none;
  }
}

.avatar {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  background-color: #f8f8f8;
  margin-right: 5px;
  border: none;
}

.like-box {
  display: flex;
  justify-content: center;
}
/*美化 - 文章详情 - 底部的评论页面 */
// 外层容器

// 发布评论的盒子 - 1
.add-cmt-box {
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  box-sizing: border-box;
  background-color: white;
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 46px;
  line-height: 46px;
  padding-left: 10px;
  .ipt-cmt-div {
    flex: 1;
    border: 1px solid #efefef;
    border-radius: 15px;
    height: 30px;
    font-size: 12px;
    line-height: 30px;
    padding-left: 15px;
    margin-left: 10px;
    background-color: #f8f8f8;
  }
  .icon-box {
    width: 40%;
    display: flex;
    justify-content: space-evenly;
    line-height: 0;
  }
}

.child {
  width: 20px;
  height: 20px;
  background: #f2f3f5;
}

// 发布评论的盒子 - 2
.cmt-box {
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 80px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 12px;
  padding-left: 10px;
  box-sizing: border-box;
  background-color: white;
  textarea {
    flex: 1;
    height: 66%;
    border: 1px solid #efefef;
    background-color: #f8f8f8;
    resize: none;
    border-radius: 6px;
    padding: 5px;
  }
  .van-button {
    height: 100%;
    border: none;
  }
}
.art-cmt-container-1 {
  padding-bottom: 46px;
}
.art-cmt-container-2 {
  padding-bottom: 80px;
}
</style>
